<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="采购计划编号" prop="stockNum">-->
<!--      <el-input v-model="dataForm.stockNum" placeholder="采购计划编号"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="采购计划名称" prop="stockName">
      <el-input v-model="dataForm.stockName" placeholder="采购计划名称"></el-input>
    </el-form-item>
    <el-form-item label="编制人序号" prop="authorId">
      <el-input v-model="dataForm.authorId" placeholder="编制人序号"></el-input>
    </el-form-item>
    <el-form-item label="编制人" prop="author">
      <el-input v-model="dataForm.author" placeholder="编制人"></el-input>
    </el-form-item>
    <el-form-item label="采购类型" prop="stockType">
      <el-input v-model="dataForm.stockType" placeholder="采购类型"></el-input>
    </el-form-item>
    <el-form-item label="预算金额(元)" prop="budget">
      <el-input v-model="dataForm.budget" placeholder="预算金额(元)"></el-input>
    </el-form-item>
    <el-form-item label="计划询价开始时间" prop="startDate">
      <el-input v-model="dataForm.startDate" placeholder="计划询价开始时间"></el-input>
    </el-form-item>
    <el-form-item label="计划报价截止时间" prop="endDate">
      <el-input v-model="dataForm.endDate" placeholder="计划报价截止时间"></el-input>
    </el-form-item>
    <el-form-item label="下达时间" prop="submitDate">
      <el-input v-model="dataForm.submitDate" placeholder="下达时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="部长序号" prop="leaderId">
      <el-input v-model="dataForm.leaderId" placeholder="部长序号"></el-input>
    </el-form-item>
    <el-form-item label="部长" prop="leader">
      <el-input v-model="dataForm.leader" placeholder="部长"></el-input>
    </el-form-item>
    <el-form-item label="部长是否同意" prop="leadAgree">
      <el-input v-model="dataForm.leadAgree" placeholder="部长是否同意"></el-input>
    </el-form-item>
    <el-form-item label="部长审核意见" prop="leadOpinion">
      <el-input v-model="dataForm.leadOpinion" placeholder="部长审核意见"></el-input>
    </el-form-item>
    <el-form-item label="部长审核时间" prop="leadDate">
      <el-input v-model="dataForm.leadDate" placeholder="部长审核时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          stockNum: '',
          stockName: '',
          authorId: '',
          author: '',
          stockType: '',
          budget: '',
          startDate: '',
          endDate: '',
          submitDate: '',
          remark: '',
          leaderId: '',
          leader: '',
          leadAgree: '',
          leadOpinion: '',
          leadDate: ''
        },
        dataRule: {
          stockNum: [
            { required: true, message: '采购计划编号不能为空', trigger: 'blur' }
          ],
          stockName: [
            { required: true, message: '采购计划名称不能为空', trigger: 'blur' }
          ],
          authorId: [
            { required: true, message: '编制人序号不能为空', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '编制人不能为空', trigger: 'blur' }
          ],
          stockType: [
            { required: true, message: '采购类型不能为空', trigger: 'blur' }
          ],
          budget: [
            { required: true, message: '预算金额(元)不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '计划询价开始时间不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '计划报价截止时间不能为空', trigger: 'blur' }
          ],
          submitDate: [
            { required: true, message: '下达时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          leaderId: [
            { required: true, message: '部长序号不能为空', trigger: 'blur' }
          ],
          leader: [
            { required: true, message: '部长不能为空', trigger: 'blur' }
          ],
          leadAgree: [
            { required: true, message: '部长是否同意不能为空', trigger: 'blur' }
          ],
          leadOpinion: [
            { required: true, message: '部长审核意见不能为空', trigger: 'blur' }
          ],
          leadDate: [
            { required: true, message: '部长审核时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/stock/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.stockNum = data.stock.stockNum
                this.dataForm.stockName = data.stock.stockName
                this.dataForm.authorId = data.stock.authorId
                this.dataForm.author = data.stock.author
                this.dataForm.stockType = data.stock.stockType
                this.dataForm.budget = data.stock.budget
                this.dataForm.startDate = data.stock.startDate
                this.dataForm.endDate = data.stock.endDate
                this.dataForm.submitDate = data.stock.submitDate
                this.dataForm.remark = data.stock.remark
                this.dataForm.leaderId = data.stock.leaderId
                this.dataForm.leader = data.stock.leader
                this.dataForm.leadAgree = data.stock.leadAgree
                this.dataForm.leadOpinion = data.stock.leadOpinion
                this.dataForm.leadDate = data.stock.leadDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/stock/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'stockNum': this.dataForm.stockNum,
                'stockName': this.dataForm.stockName,
                'authorId': this.dataForm.authorId,
                'author': this.dataForm.author,
                'stockType': this.dataForm.stockType,
                'budget': this.dataForm.budget,
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'submitDate': this.dataForm.submitDate,
                'remark': this.dataForm.remark,
                'leaderId': this.dataForm.leaderId,
                'leader': this.dataForm.leader,
                'leadAgree': this.dataForm.leadAgree,
                'leadOpinion': this.dataForm.leadOpinion,
                'leadDate': this.dataForm.leadDate
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
